<template>
    <div>
        <el-container>
            <el-main>
                <div
                    style="margin: 200px auto; background: linear-gradient(rgb(238, 232, 232),rgb(233, 215, 215)); width: 400px; height: 400px; padding: 20px; border-radius: 20px;">
                    <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注册</b></div>
                    <el-form :model="customer" :rules="rules" ref="customerForm">
                        <el-form-item prop="username" label="用户名" label-width="80px">
                            <el-input size="medium" style="" prefix-icon="el-icon-user"
                                v-model="customer.username"></el-input>
                        </el-form-item>
                        <el-form-item prop="password" label="密码" label-width="80px">
                            <el-input size="medium" style="" prefix-icon="el-icon-lock" show-password
                                v-model="customer.password"></el-input>
                        </el-form-item>
                        <el-form-item prop="name" label="姓名" label-width="80px">
                            <el-input size="medium" style="" prefix-icon="el-icon-user-solid" 
                                v-model="customer.name"></el-input>
                        </el-form-item>
                        <el-form-item prop="phone" label="手机号" label-width="80px">
                            <el-input size="medium" style="" prefix-icon="el-icon-phone" 
                                v-model="customer.phone"></el-input>
                        </el-form-item>
                        <el-form-item style="margin: 10px 0; text-align: right">
                            <el-button type="warning" size="small" autocomplete="off" @click="toRegister">注册</el-button>
                            <el-button type="primary" size="small" autocomplete="off" @click="toLogin">返回登录</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-main>
    
        </el-container>
    </div>
</template>

<script>
export default {
    name: "Register",
    data() {
        return {
            customer: {},
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度为11位手机号', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        toRegister() {
            //发起注册请求 校验数据是否合法
            this.$refs['customerForm'].validate((valid) => {
                if (valid) {
                    this.request.post("/customer/register",this.customer).then(res=>{
                        console.log("当前res==>",res)
                        if(res.code==200){
                            this.$message.success("注册成功");
                            this.$refs['customerForm'].resetFields();
                            this.$router.push("/front/login")
                        }else{
                            this.$message.error(res.msg);
                        }
                    })
                }
            });
        },
        toLogin(){
            //跳转登录界面
            this.$router.push("/front/login")
        }
        
    }
}
</script>

<style lang="less" scoped>
.el-main {
    min-height: 600px;
    background-image: url(../../assets/bj.jpeg);
    background-size: 100% 80%;
    background-attachment: fixed;
}

.el-header,
.el-footer {
    background-color: #2F2F2F;
    color: #333;
    text-align: center;
    color: aliceblue;
    font-size: 12px;
    line-height: 20px;
}
</style>